package cn.dc406.labc406mgr.controller;

import cn.dc406.labc406mgr.entity.*;
import cn.dc406.labc406mgr.mapper.ItemsMapper;
import cn.dc406.labc406mgr.mapper.ItemsWapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class ItemController {
    @Autowired
    ItemsMapper itemsMapper;
    @Autowired
    ItemsWapper itemsWapper;





    @CrossOrigin
    @RequestMapping("/getItems")
    List<Item> getItems()
    {
        return itemsMapper.selectItemList();
    }

    @CrossOrigin
    @RequestMapping("/getItemsBetween")
    List<Item> getItemsBetween(int limit,int offset)//记录数，起始位置
    {
        System.out.println("limit,offset="+limit+","+offset);
        List<Item> items=itemsMapper.selectItemsLimited(limit,offset);
        System.out.println(items);
        return items;
    }

    @CrossOrigin
    @RequestMapping("/getItemsSize")
    int getItemsSize()//记录数，起始位置
    {

        int size=itemsMapper.selectItemsSize();
//        System.out.println(size);
        return size;
    }

    @CrossOrigin
    @RequestMapping("/getItemDetailByName")
    ItemDetail getItemDetailByName(String name)
    {
//        System.out.println(name);
        return itemsMapper.selectItemDetailByName(name);
    }

    @CrossOrigin
    @RequestMapping("/addApplication")
    int addApplication(@RequestBody LendDetail lendDetail, HttpSession httpSession)
    {
        User user=(User) httpSession.getAttribute("user");
        String username= user.getUsername();
        lendDetail.setUsername(username);
        try {
//            System.out.println(lendDetail);
            return itemsMapper.insertLendDetails(lendDetail);
        }catch (Exception e)
        {
            e.printStackTrace();
            return 0;
        }
    }

    @CrossOrigin()
    @ResponseBody
    @RequestMapping("/selectAll")
    public List<Items> selectAll()
    {

        return itemsWapper.SelectAll();
    }
}
